import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { CasesService } from './cases.service';
import { CreateCaseDto } from './dto/create-case.dto';
import { UpdateCaseDto } from './dto/update-case.dto';
import { SwaggerErr, SwaggerOk } from 'src/decorators/swagger.decorator';
import { Api } from 'src/utils/api';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

type TSortData = {
  id:number;
  sort:number;
}

@ApiTags('cases测试步骤的接口')
@Controller('cases')
export class CasesController {
  constructor(private readonly casesService: CasesService) { }

  @SwaggerOk()
  @Post()
  async create(@Body() createCaseDto: CreateCaseDto) {
    await this.casesService.create(createCaseDto);
    return Api.ok();
  }

  @Post('/updateSort')
  async updateSort(@Body() data:TSortData[]){
    if(data.length > 0){
      data.map( d=>{
        const res = this.casesService.updateSort(d.id, d.sort);
      });
    }
    return Api.ok();
  }

  @Get(':sid')
  @SwaggerOk(Array<CreateCaseDto>)
  findAll(@Param('sid') sid: string) {
    //根据fid查询所有的
    return this.casesService.findAll(+sid);
  }

  @ApiOperation({summary:'根据id复制testCase'})
  @Get('/copy/:id')
  async copyCase(@Param('id') id:string){
    await this.casesService.copy(+id);
    return Api.ok();
  }

  @Patch(':id')
  @SwaggerOk()
  update(@Param('id') id: string, @Body() updateCaseDto: UpdateCaseDto) {
    return this.casesService.update(+id, updateCaseDto);
  }

  @Delete(':id')
  @SwaggerOk()
  remove(@Param('id') id: string) {
    return this.casesService.remove(+id);
  }
}
